<template>
  <div id="globalHeaderPage">
    <a-row :wrap="false">
      <a-col flex="200px">
        <div class="titleBar">
          <img src="../assets/logo.png" class="logo" />
          <div class="title">星友学知识交流平台</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @Click="menuItemClick"
        />
      </a-col>
      <a-col flex="100px">
        <div class="login-user-status">
          <div v-if="loginUserStore.loginUser.id">
            {{ loginUserStore.loginUser.userName ?? "无名" }}
          </div>
          <div v-else>
            <a-button href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from "vue";
import { MenuProps } from "ant-design-vue";
import { useRouter } from "vue-router";
import router from "@/router";
import { getCurrentUser } from "@/api/user";
import { useLoginUserStore } from "@/store/user";

const current = ref<string[]>(["/"]);
const loginUserStore = useLoginUserStore();
const menuItemClick = function ({ key }: { key: string }) {
  router.push({
    path: key,
  });
};

//监听路由变化
router.afterEach((to, from, failure) => {
  current.value = [to.path];
});

const items = ref<MenuProps["items"]>([
  {
    key: "/",
    // icon: () => h(MailOutlined),
    label: "主页",
    title: "主页",
  },
  {
    key: "/essay",
    // icon: () => h(MailOutlined),
    label: "交流",
    title: "交流",
  },
  {
    key: "/team",
    // icon: () => h(AppstoreOutlined),
    label: "队伍",
    title: "队伍",
  },
  {
    key: "/application",
    // icon: () => h(SettingOutlined),
    label: "应用",
    title: "应用",
  },
  {
    key: "/recommend",
    // icon: () => h(SettingOutlined),
    label: "推荐",
    title: "推荐",
  },
  {
    key: "/blog",
    // icon: () => h(SettingOutlined),
    label: h(
      "a",
      { href: "https://juejin.cn/user/2432535756017246", target: "_blank" },
      "个人博客"
    ),
    title: "管理页面",
  },
  {
    key: "/user/login",
    // icon: () => h(SettingOutlined),
    label: "登录",
    title: "登录",
  },
  {
    key: "/user/register",
    // icon: () => h(SettingOutlined),
    label: "注册",
    title: "注册",
  },
  {
    key: "/admin/userManager",
    // icon: () => h(SettingOutlined),
    label: "用户管理中心",
    title: "用户管理中心",
  },
  {
    key: "/user/info",
    // icon: () => h(SettingOutlined),
    label: "个人信息",
    title: "个人信息",
  },
]);
</script>
<style scoped>
#globalHeaderPage .titleBar {
  display: flex;
  align-items: center;
}

#globalHeaderPage .logo {
  height: 40px;
}
</style>
